﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class MenuDal
    {
        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Model.Menu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Menu(");
            strSql.Append("Id,Name,Channel,URL,Target,Title,Items,OrderNum,IsSystem,Shown)");
            strSql.Append(" values (");
            strSql.Append("@Id,@Name,@Channel,@URL,@Target,@Title,@Items,@OrderNum,@IsSystem,@Shown)");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Channel", SqlDbType.NVarChar,255),
					new SqlParameter("@URL", SqlDbType.NVarChar,50),
					new SqlParameter("@Target", SqlDbType.NVarChar,50),
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@Items", SqlDbType.Int,4),
					new SqlParameter("@OrderNum", SqlDbType.Int,4),
					new SqlParameter("@IsSystem", SqlDbType.Bit,1),
					new SqlParameter("@Shown", SqlDbType.Bit,1)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Channel;
            parameters[3].Value = model.URL;
            parameters[4].Value = model.Target;
            parameters[5].Value = model.Title;
            parameters[6].Value = model.Items;
            parameters[7].Value = model.OrderNum;
            parameters[8].Value = model.IsSystem;
            parameters[9].Value = model.Shown;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Model.Menu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Menu set ");
            strSql.Append("Id=@Id,");
            strSql.Append("Name=@Name,");
            strSql.Append("Channel=@Channel,");
            strSql.Append("URL=@URL,");
            strSql.Append("Target=@Target,");
            strSql.Append("Title=@Title,");
            strSql.Append("Items=@Items,");
            strSql.Append("OrderNum=@OrderNum,");
            strSql.Append("IsSystem=@IsSystem,");
            strSql.Append("Shown=@Shown");
            strSql.Append(" where ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Channel", SqlDbType.NVarChar,255),
					new SqlParameter("@URL", SqlDbType.NVarChar,50),
					new SqlParameter("@Target", SqlDbType.NVarChar,50),
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@Items", SqlDbType.Int,4),
					new SqlParameter("@OrderNum", SqlDbType.Int,4),
					new SqlParameter("@IsSystem", SqlDbType.Bit,1),
					new SqlParameter("@Shown", SqlDbType.Bit,1)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Channel;
            parameters[3].Value = model.URL;
            parameters[4].Value = model.Target;
            parameters[5].Value = model.Title;
            parameters[6].Value = model.Items;
            parameters[7].Value = model.OrderNum;
            parameters[8].Value = model.IsSystem;
            parameters[9].Value = model.Shown;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete T_Menu ");
            strSql.Append(" where ");
            SqlParameter[] parameters = {
};

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Menu GetModel()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Name,Channel,URL,Target,Title,Items,OrderNum,IsSystem,Shown from T_Menu ");
            strSql.Append(" where ");
            SqlParameter[] parameters = {
};

            Model.Menu model = new Model.Menu();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = new Guid(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                model.Channel = ds.Tables[0].Rows[0]["Channel"].ToString();
                model.URL = ds.Tables[0].Rows[0]["URL"].ToString();
                model.Target = ds.Tables[0].Rows[0]["Target"].ToString();
                model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                if (ds.Tables[0].Rows[0]["Items"].ToString() != "")
                {
                    model.Items = int.Parse(ds.Tables[0].Rows[0]["Items"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderNum"].ToString() != "")
                {
                    model.OrderNum = int.Parse(ds.Tables[0].Rows[0]["OrderNum"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsSystem"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsSystem"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsSystem"].ToString().ToLower() == "true"))
                    {
                        model.IsSystem = true;
                    }
                    else
                    {
                        model.IsSystem = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["Shown"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["Shown"].ToString() == "1") || (ds.Tables[0].Rows[0]["Shown"].ToString().ToLower() == "true"))
                    {
                        model.Shown = true;
                    }
                    else
                    {
                        model.Shown = false;
                    }
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Name,Channel,URL,Target,Title,Items,OrderNum,IsSystem,Shown ");
            strSql.Append(" FROM T_Menu ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "T_Menu";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法

    }
}
